﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Linq;
using System.Collections;
using System.Reflection;
using UMC.Wiki.Entities;
using UMC.Web;

namespace UMC.Wiki.Activities
{
    class SubjectItemActivity : WebActivity
    {

        public override void ProcessActivity(WebRequest request, WebResponse response)
        {
            var pkd = UMC.Data.Utility.UInt64Parse(this.AsyncDialog("Id", g =>
             {
                 return this.DialogValue("none");
             })) ?? 0;

            var user = this.Context.Token.Identity();

            var projectItem = DataFactory.Instance().ProjectItem(pkd);
            if (projectItem == null)
            {
                this.Prompt("未找到项目");
            }

            var project = DataFactory.Instance().Project(projectItem.project_id.Value);

            var status = -1;
            var member = DataFactory.Instance().ProjectMember(projectItem.project_id.Value, user.Id.Value);

            if (member != null)
            {
                switch (member.AuthType)
                {
                    case WebAuthType.Admin:
                        status = 1;
                        break;
                    default:
                    case WebAuthType.Guest:
                        status = 0;
                        break;
                    case WebAuthType.User:
                        status = 1;
                        break;
                }
            }
            var Portfolios = DataFactory.Instance().Portfolio(projectItem).OrderBy(c => c.Sequence ?? 0).ToArray();
            var userids = new List<Guid>();
            var subs = new List<Subject>();
            if (Portfolios.Length > 0)
            {
                var subs2 = DataFactory.Instance().Subject(Portfolios).OrderBy(r => r.Seq ?? 0);
                var lcount = subs2.Count();
                if (projectItem.SubjectTotal != lcount)
                {
                    DataFactory.Instance().Put(new ProjectItem { Id = projectItem.Id.Value, SubjectTotal = lcount });
                }

                UMC.Data.Utility.Each(subs2, dr =>
                {

                    if (status == 0 && dr.Status < 1)
                    {
                        return;
                    }
                    subs.Add(dr);
                    if (dr.user_id.HasValue && userids.Exists(g => g == dr.user_id) == false)
                    {
                        userids.Add(dr.user_id.Value);
                    }
                    if (dr.last_user_id.HasValue && userids.Exists(g => g == dr.last_user_id) == false)
                    {
                        userids.Add(dr.last_user_id.Value);

                    }
                });
            }

            var webr = Web.WebResource.Instance();
            var users = new List<WebMeta>();
            if (userids.Count > 0)
            {

                UMC.Data.Utility.Each(userids, dr =>
                {
                    users.Add(new WebMeta().Put("src", webr.ImageResolve(request.Url, dr, "1", "4")).Put("id", dr));
                });
            }
            var data = new List<WebMeta>();
            foreach (var p in Portfolios)
            {
                var meta = new WebMeta();
                meta.Put("id", p.Id).Put("text", p.Caption);
                var csubs = subs.FindAll(s => s.portfolio_id == p.Id);
                var dcsub = new List<WebMeta>();
                foreach (var cs in csubs)
                {
                    var mta = new WebMeta().Put("id", cs.Id).Put("text", cs.Title);
                    if (String.IsNullOrEmpty(cs.Code))
                    {
                        cs.Code = "none";
                    }
                    mta.Put("code", cs.Code);
                    mta.Put("path", String.Format("{0}/{1}/{2}", project.Code, projectItem.Code, cs.Code));
                    if (cs.Status < 1)
                    {
                        if (status == 1)
                        {
                            mta.Put("state", "未发布");
                        }
                        else
                        {
                            continue;
                        }
                    }
                    if (status == 1)
                    {
                        mta.Put("visible", (cs.Visible ?? 0) == 1);
                    }
                    dcsub.Add(mta); ;
                }
                if (dcsub.Count > 0)
                {
                    meta.Put("subs", dcsub);

                    data.Add(meta);
                }
            }
            var root = new WebMeta().Put("id", projectItem.Id).Put("caption", projectItem.Caption).Put("path", String.Format("{0}/{1}", project.Code, projectItem.Code)).Put("data", data).Put("users", users);

            if (status == 1 && (projectItem.PublishTime ?? 0) + 600 < UMC.Data.Utility.TimeSpan())
            {
                root.Put("releaseId", projectItem.Id.ToString());

                DataFactory.Instance().Put(new ProjectItem { Id = projectItem.Id, PublishTime = UMC.Data.Utility.TimeSpan() });
            }
            root.Put("Keywords", projectItem.Keywords);
            root.Put("Description", projectItem.Description);
            response.Redirect(root);


        }

    }
}